<template>
  <div>
    <el-dialog
      title="积分充值入口（积分日元1：1）"
      destroy-on-close
      append-to-body
      :visible.sync="show"
      width="600px"
      :before-close="cancel">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="充值点数" prop="orderAmount">
          <el-input type="number" v-model.number="form.orderAmount" />
        </el-form-item>
        <el-form-item label="充值理由" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="7" show-word-limit maxlength="500" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handlePay"> 支付 </el-button>
      </div>
    </el-dialog>
    <Pay
      :needFee="false"
      :show.sync="pay.show"
      @getList="getList"
      :useDefaultDetail="false"
      :formData="pay.formData">
      <template #detail="{ detail }">
        <div class="payMessage">
          <el-row :gutter="20">
            <el-col :span="12">{{ $t('订单数量') }} :{{ detail.payBillNum }}</el-col>
            <el-col :span="12">
              {{ $t('金额') }} :{{ detail.orderAmount ? (+detail.orderAmount).toFixed() : '' }}JPY
            </el-col>
          </el-row>
        </div>
      </template>
    </Pay>
  </div>
</template>

<script>
// import Pay from './pay.vue'
import Pay from '@/components/Pay/index.vue'
import { sellerPointsPayInfoByPlatform } from '@/api/bill/platDistributerPoint'

export default {
  components: {
    Pay,
  },
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  props: {
    /**
     * data
     */
    formData: {
      type: Object,
      default: () => ({}),
    },
    /**
     * 标题
     */
    title: {
      type: String,
      required: true,
    },
    /**
     * type
     */
    type: {
      type: [String, Number],
      required: true,
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      pay: {
        show: false,
        type: '',
        formData: {},
        idList: [],
      },
      queryParams: {},
      form: {},
      // 表单校验
      rules: {
        remark: [{ required: true, message: '请输入', trigger: 'blur' }],
        orderAmount: [{ required: true, message: '请输入', trigger: 'blur' }],
      },
    }
  },
  watch: {
    show: function (newValue) {
      if (newValue) {
        Object.keys(this.form).forEach((key) => (this.form[key] = ''))
      }
    },
  },
  methods: {
    getList() {
      this.$emit('getList')
      this.cancel()
    },
    handlePay() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          const data = {
            amount: this.form.orderAmount,
            remark: this.form.remark,
            /**租户ID(当前行的distrId) */
            distrId: this.formData.distrId,
            /**租户类型（当前行的Type） */
            // distrIdType: this.formData.type,
          }
          sellerPointsPayInfoByPlatform(data).then((res) => {
            const { code, data, msg } = res
            if (code === 200) {
              this.$message({ type: 'success', message: msg})
              this.getList()
            }
          })
        }
      })
    },

    cancel() {
      this.$emit('update:show', false)
    },
  },
}
</script>
<style scoped lang="scss">
.payMessage {
  text-align: center;
  font-size: 16px;
  color: black;
  font-weight: 700;
  margin-bottom: 10px;
}
</style>
